﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.IO.IsolatedStorage;
using System.Xml.Linq;
using System.IO;
using System.Linq;
using System.Collections.Generic;
using HomeBuyerTools.FinanceProperties;
namespace HomeBuyerTools
{
    public  class Settings
    {
        XDocument xdoc1;
        IsolatedStorageFile isostore = IsolatedStorageFile.GetUserStoreForApplication();
        IsolatedStorageFile isostore1 = IsolatedStorageFile.GetUserStoreForApplication();
        IsolatedStorageFile ISF = IsolatedStorageFile.GetUserStoreForApplication();
        IsolatedStorageFileStream isostream;
        IsolatedStorageFileStream isostream1;
     
        public void AddInterest(double InterestRate,double MortgagePrincipal)
        {
            List<SettingsProperties> myprop = new List<SettingsProperties>();
            XDocument xdoc;
            if (!isostore.FileExists("Settings.xml"))
            {
                isostream = new IsolatedStorageFileStream("Settings.xml", FileMode.Create, isostore);
                
                xdoc = new XDocument(new XElement("Settings",
                    new XElement("value", new XAttribute("id", 1),
                                  new XElement("InterestRate", 3.5),
                                  new XElement("MortgagePrincipal", 648))));
                // new XElement("MortgagePrincipal", MortgagePrincipal)));
                xdoc.Save(isostream);
                isostream.Close();
            }
            else
            {

                isostream = new IsolatedStorageFileStream("Settings.xml", FileMode.Open, isostore);
                xdoc = XDocument.Load(isostream);
                isostream.Close();
                var findEle = from i in xdoc.Descendants("value") select i;
                foreach (var ele in findEle)
                {

                    if (InterestRate != 0)
                    {
                        if (ele.Element("InterestRate") != null)
                            ele.Element("InterestRate").Remove();
                        XElement doc1 = new XElement("InterestRate", InterestRate);
                        ele.Add(new XElement(doc1));
                    }
                    if (MortgagePrincipal != 0)
                    {
                        if (ele.Element("MortgagePrincipal") != null)
                            ele.Element("MortgagePrincipal").Remove();

                        XElement doc2 = new XElement("MortgagePrincipal", MortgagePrincipal);

                        ele.Add(new XElement(doc2));
                    }
                }
                isostream = new IsolatedStorageFileStream("Settings.xml", FileMode.Create, isostore);
                xdoc.Save(isostream);
                isostream.Close();
            }
        }
        public void AddTotalExpensiveInterest(string ExpensiveName, string Amount,DateTime FromDateTime,DateTime ToDate)
        {
            try
            {

                List<TotalExpensiveProperties> totalExp = new List<TotalExpensiveProperties>();
                if (!isostore.FileExists("TotalExpensive.xml"))
                {
                    isostream = new IsolatedStorageFileStream("TotalExpensive.xml", FileMode.Create, isostore);
                    xdoc1 = new XDocument(new XElement("TotalExpensive",
                        new XElement("value",
                                      new XElement("ExpensiveName", ExpensiveName),
                                      new XElement("Amount", Amount),
                                      new XElement("FromDateTime", FromDateTime),
                                       new XElement("ToDate",ToDate))));
                    // new XElement("MortgagePrincipal", MortgagePrincipal)));
                    xdoc1.Save(isostream);
                    isostream.Close();

                }
                else
                {
                    App.xdoc.Root.Add(
                                                             (new XElement("value",
                                                                  new XElement("ExpensiveName", ExpensiveName),
                                                                  new XElement("Amount", Amount),
                                                                   new XElement("FromDateTime", FromDateTime),
                                                                    new XElement("ToDate", ToDate))));
                    isostream = new IsolatedStorageFileStream("TotalExpensive.xml", FileMode.Create, isostore);
                    App.xdoc.Save(isostream);
                    isostream.Close();
                }
            }
            catch (Exception ex)
            { 
            }
        }
        //public void AddPrincipal(double MortgagePrincipal)
        //{
        //    if (!isostore.FileExists("Settings.xml"))
        //    {
        //        isostream = new IsolatedStorageFileStream("Settings.xml", FileMode.Create, isostore);
        //        xdoc = new XDocument(new XElement("Settings", new XAttribute("id", 1),
        //                          new XElement("MortgagePrincipal", MortgagePrincipal)));
        //        // new XElement("MortgagePrincipal", MortgagePrincipal)));
        //        xdoc.Save(isostream);
        //        isostream.Close();
        //    }
        //    else
        //    {
        //        isostream = new IsolatedStorageFileStream("Settings.xml", FileMode.Open, isostore);
        //        xdoc = XDocument.Load(isostream);
        //        isostream.Close();
        //        XElement doc1 = new XElement("Settings", new XAttribute("id", 1),
        //                        new XElement("MortgagePrincipal", MortgagePrincipal));
        //        xdoc.Root.Add(doc1);
        //        isostream = new IsolatedStorageFileStream("Settings1.xml", FileMode.Create, isostore);
        //        xdoc.Save(isostream);
        //        isostream.Close();
        //    }
        //}

        public List<SettingsProperties> loadInterest()
        {
            XDocument xdoc;
            List<SettingsProperties> p = new List<SettingsProperties>();
            SettingsProperties prop = new SettingsProperties();

            if (isostore.FileExists("Settings.xml"))
            {
                isostream = new IsolatedStorageFileStream("Settings.xml", FileMode.Open, isostore);
                xdoc = XDocument.Load(isostream);
                isostream.Close();
                var xquery = from a in xdoc.Descendants("value") select a;
               
                foreach (XElement item in xquery)
                {
                    prop.InterestRate = Convert.ToDouble(item.Element("InterestRate").Value);
                    prop.MortgagePrincipal = Convert.ToDouble(item.Element("MortgagePrincipal").Value);
                    p.Add(prop);
                }

            }
            return p;
        }
        public  List<TotalExpensiveProperties> loadTotalExpensive()
        {
            XDocument xdoc;
           
           
            List<TotalExpensiveProperties> p1 = new List<TotalExpensiveProperties>();
            if (isostore.FileExists("TotalExpensive.xml"))
            {
                isostream = new IsolatedStorageFileStream("TotalExpensive.xml", FileMode.Open, isostore);
                xdoc = XDocument.Load(isostream);
                isostream.Close();
                var xquery = from a in xdoc.Descendants("value") select a;

                foreach (XElement item in xquery)
                {
                     TotalExpensiveProperties prop1 = new TotalExpensiveProperties();
                    prop1.ExpensiveName = item.Element("ExpensiveName").Value;
                    prop1.Amount = item.Element("Amount").Value;
                    prop1.FromDateTime = Convert.ToDateTime(item.Element("FromDateTime").Value);
                    prop1.ToDate = Convert.ToDateTime(item.Element("ToDate").Value);
                   
                 p1.Add(prop1);
                }
                App.xdoc =xdoc;
            }
            return p1;
        }

        public List<TotalExpensiveProperties> RemovieItems(string name , string amount)
    {
        List<TotalExpensiveProperties> p2 = new List<TotalExpensiveProperties>();
        try
        {
            IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication();
            IsolatedStorageFileStream isoStream;
            //open selected file
            isoStream = new IsolatedStorageFileStream("TotalExpensive.xml", System.IO.FileMode.Open, System.IO.FileAccess.Read, isoStore);
            XDocument xml = XDocument.Load(isoStream);
            isoStream.Close();
            //Find item and remove it
            App.xdoc.Root.Elements("value")
            .Where(item => item.Element("ExpensiveName").Value == name && item.Element("Amount").Value == amount).FirstOrDefault().Remove();
            isoStream.Close();  //Seems unnecessary but it's needed.
            //Write xml file
            isoStream = new IsolatedStorageFileStream("TotalExpensive.xml", FileMode.Create, FileAccess.Write, isoStore);
            App.xdoc.Save(isoStream);
            isoStream.Close();
        }
        catch (Exception ex)
        {
        }
        return p2;
    }
        //public List<SettingsProperties> loadMortgage()
        //{
        //    List<SettingsProperties> p = new List<SettingsProperties>();
        //    SettingsProperties prop = new SettingsProperties();
        //    if (isostore.FileExists("Settings1.xml"))
        //    {
        //        isostream = new IsolatedStorageFileStream("Settings1.xml", FileMode.Open, isostore);
        //        xdoc = XDocument.Load(isostream);
        //        isostream.Close();
        //        var xquery = from a in xdoc.Descendants("Settings") select a;

        //        foreach (XElement item in xquery)
        //        {
        //            prop.MortgagePrincipal = Convert.ToDouble(item.Element("MortgagePrincipal").Value);
        //            //prop.MortgagePrincipal = Convert.ToDouble(item.Element("MortgagePrincipal"));
        //            p.Add(prop);
        //        }

        //    }

        //    return p;
        //}


    }
}
